from django.contrib import admin
from .models import Article, Category, Tag


@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
    list_display = (
        'title', 'author', 'status', 'is_top', 'is_public', 'views', 'pub_time',
    )
    list_filter = (
        'status', 'is_top', 'is_public', 'author', 'pub_time', 'category',
    )
    search_fields = ('title', 'content', 'summary')
    ordering = ('-is_top', '-pub_time')
    filter_horizontal = ('tags',)
    date_hierarchy = 'pub_time'
    readonly_fields = ('views', 'slug', 'pub_time', 'update_time')

    fieldsets = (
        ("基本信息", {
            "fields": ("title", "slug", "author", "category", "tags")
        }),
        ("内容设置", {
            "fields": ("cover", "content", "summary", "is_show_toc")
        }),
        ("状态控制", {
            "fields": ("status", "is_top", "is_public", "allow_comments")
        }),
        ("时间与阅读", {
            "fields": ("pub_time", "update_time", "views")
        }),
    )


@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    list_display = ('name', 'slug')
    search_fields = ('name',)
    readonly_fields = ('slug',)


@admin.register(Tag)
class TagAdmin(admin.ModelAdmin):
    list_display = ('name', 'slug')
    search_fields = ('name',)
    readonly_fields = ('slug',)
